package org.micropayment.model;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.oauth.common.signature.RSAKeySecret;
import org.springframework.security.oauth.common.signature.SharedConsumerSecret;
import org.springframework.security.oauth.common.signature.SignatureSecret;
import org.springframework.security.oauth.provider.ConsumerDetails;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;

/**
 *
 * @author jj
 */
@Entity
@Table(name = "MicropaymentConsumer")
@NamedQueries({
    @NamedQuery(name = "MicropaymentConsumer.findAll", query = "SELECT m FROM MicropaymentConsumer m"),
    @NamedQuery(name = "MicropaymentConsumer.findById", query = "SELECT m FROM MicropaymentConsumer m WHERE m.id = :id"),
    @NamedQuery(name = "MicropaymentConsumer.findByConsumerKey", query = "SELECT m FROM MicropaymentConsumer m WHERE m.consumerKey = :consumerKey"),
    @NamedQuery(name = "MicropaymentConsumer.findByConsumerSecret", query = "SELECT m FROM MicropaymentConsumer m WHERE m.consumerSecret = :consumerSecret"),
    @NamedQuery(name = "MicropaymentConsumer.findByConsumerName", query = "SELECT m FROM MicropaymentConsumer m WHERE m.consumerName = :consumerName"),
    @NamedQuery(name = "MicropaymentConsumer.findByTypeofsecret", query = "SELECT m FROM MicropaymentConsumer m WHERE m.typeofsecret = :typeofsecret"),
    @NamedQuery(name = "MicropaymentConsumer.findByResourceName", query = "SELECT m FROM MicropaymentConsumer m WHERE m.resourceName = :resourceName"),
    @NamedQuery(name = "MicropaymentConsumer.findByResourceDescription", query = "SELECT m FROM MicropaymentConsumer m WHERE m.resourceDescription = :resourceDescription")})
public class MicropaymentConsumer implements Serializable, ConsumerDetails {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    @Basic(optional = false)
    @Column(name = "consumerKey")
    private String consumerKey;
    @Basic(optional = false)
    @Column(name = "signatureMethod")
    private SignatureMethod signatureMethod;
    @Basic(optional = false)
    @Column(name = "consumerSecret")
    private String consumerSecret;
    @Basic(optional = false)
    @Column(name = "consumerName")
    private String consumerName;
    @Basic(optional = false)
    @Column(name = "typeofsecret")
    @Enumerated(value = EnumType.STRING)
    private TypeOfSecret typeofsecret;
    @Basic(optional = false)
    @Column(name = "resourceName")
    private String resourceName;
    @Column(name = "resourceDescription")
    private String resourceDescription;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "micropaymentConsumerId")
    private Collection<Token> tokenCollection;
    @JoinColumn(name = "microcreditUserId", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private MicropaymentUser microcreditUserId;

    public MicropaymentConsumer() {
    }

    public MicropaymentConsumer(Integer id) {
        this.id = id;
    }

    public MicropaymentConsumer(Integer id, String consumerKey,
            String consumerSecret, TypeOfSecret typeofsecret, String resourceName) {
        this.id = id;
        this.consumerKey = consumerKey;
        this.consumerSecret = consumerSecret;
        this.typeofsecret = typeofsecret;
        this.resourceName = resourceName;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getConsumerKey() {
        return consumerKey;
    }

    public void setConsumerKey(String consumerKey) {
        this.consumerKey = consumerKey;
    }

    public String getConsumerSecret() {
        return consumerSecret;
    }

    public void setConsumerSecret(String consumerSecret) {
        this.consumerSecret = consumerSecret;
    }

    public TypeOfSecret getTypeofsecret() {
        return typeofsecret;
    }

    public void setTypeofsecret(TypeOfSecret typeofsecret) {
        this.typeofsecret = typeofsecret;
    }

    public String getResourceName() {
        return resourceName;
    }

    public void setResourceName(String resourceName) {
        this.resourceName = resourceName;
    }

    public String getResourceDescription() {
        return resourceDescription;
    }

    public void setResourceDescription(String resourceDescription) {
        this.resourceDescription = resourceDescription;
    }

    public Collection<Token> getTokenCollection() {
        return tokenCollection;
    }

    public void setTokenCollection(Collection<Token> tokenCollection) {
        this.tokenCollection = tokenCollection;
    }

    public MicropaymentUser getMicrocreditUserId() {
        return microcreditUserId;
    }

    public void setMicrocreditUserId(MicropaymentUser microcreditUserId) {
        this.microcreditUserId = microcreditUserId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof MicropaymentConsumer)) {
            return false;
        }
        MicropaymentConsumer other = (MicropaymentConsumer) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.micropayment.model.MicropaymentConsumer[id=" + id + "]";
    }

    public String getConsumerName() {
        return consumerName;
    }

    public SignatureSecret getSignatureSecret() {
        SignatureSecret result = null;
        switch (typeofsecret) {
            case RSAKeySecret:
                result = new RSAKeySecret(consumerSecret);
            break;
            case SharedConsumerSecret:
                result = new SharedConsumerSecret(consumerSecret);
            break;
        }
        return result;
    }

    public GrantedAuthority[] getAuthorities() {
        return new GrantedAuthority[] {};
    }

    /**
     * @return the signatureMethod
     */
    public SignatureMethod getSignatureMethod() {
        return signatureMethod;
    }

    /**
     * @param signatureMethod the signatureMethod to set
     */
    public void setSignatureMethod(SignatureMethod signatureMethod) {
        this.signatureMethod = signatureMethod;
    }

}
